package com.lankystar.logistucs.spring.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lankystar.logistucs.spring.entity.Company;
import com.lankystar.logistucs.spring.entity.TwoStatus;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.service.BrokerService;
import com.lankystar.logistucs.spring.service.TwoOrderService;

@Controller
@RequestMapping(value="/broker")
public class BrokerController {
/**
 * 上海报关行
 */
	@Autowired
	private BrokerService brokerService;
	@Autowired
	private TwoOrderService twoOrderService;
	
	@RequestMapping(value="queryOrderlist.do")
	public String queryOrderlist(Model model,HttpSession ses,String createTime){
		try {
			User user = (User) ses.getAttribute("userSession");
			Company company = user.getCompany();
			List<Map<String, Object>> maplist = brokerService.queryCargoAndTwoOrder(company.getCompanyId());
			model.addAttribute("maplist", maplist);
			model.addAttribute("createTime", createTime);
		} catch (Exception e) {
			System.out.println("查询有异常");
		}
		return "/broker/brokerlist";
	}
	
	@RequestMapping(value="receiveOrder.do")
	public String receiveOrder(RedirectAttributes attr,Integer twoOrderId){
		try {
			String createTime = brokerService.updateTwoOrder(twoOrderId, 0);
			attr.addAttribute("createTime", createTime);
		} catch (Exception e) {
			System.err.println("receiveOrder!!!");
		}
		return "redirect:/broker/queryOrderlist.do";
	}
	@RequestMapping(value="receiveOrderList.do")
	public String receiveOrderList(Model model,HttpSession ses,String createTime){
		try {
			User user = (User) ses.getAttribute("userSession");
			Company company = user.getCompany();
			List<Map<String, Object>> maplist = brokerService.queryRecelveOrder(company.getCompanyId(),createTime);
			model.addAttribute("maplist", maplist);
		} catch (Exception e) {
			System.out.println("查询有异常");
		}
		return "/broker/receiveOrderlist";
	}
	
	@RequestMapping(value="updateReceiveOrder.do")
	public String updateReceiveOrder(Model model,Integer twoOrderId,Integer twoStatusKey,RedirectAttributes attr){
		try {
			String createTime = brokerService.updateTwoOrder(twoOrderId, twoStatusKey);
			attr.addAttribute("createTime", createTime);
		} catch (Exception e) {
			System.err.println("二级订单状态更新异常");
		}
		return "redirect:/broker/receiveOrderList.do";
	}
	/**
	 * 上海报关行查询所有订单
	 * @return
	 */
	@RequestMapping(value="queryBrokerOrderlist.do")
	public String queryBrokerOrderlist(Model model,HttpSession ses){
		try {
			User user = (User) ses.getAttribute("userSession");
			Company company = user.getCompany();
			List<Map<String, Object>> maplist = brokerService.queryOrderlist(company.getCompanyId());
			model.addAttribute("maplist", maplist);
		} catch (Exception e) {
			System.err.println("上海报关行查询公司所有订单失败！！");
		}
		return "/broker/orders";
	}
	
	@RequestMapping(value="queryOrderStatuslist.do")
	public String queryOrderStatuslist(Model model,int twoOrderId){
		try {
			List<TwoStatus> twoStatuslist = twoOrderService.queryTwoStatusKeyById(twoOrderId);
			model.addAttribute("twoStatuslist", twoStatuslist);
			System.out.println("双方的公司的发生的广泛的后果的推广和体会不到你发个话");
		} catch (Exception e) {
			System.err.println("上海报关行查询公司所有订单失败！！");
		}
		return "/broker/orderStatus";
	}
}
